home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2004 May / SGI IRIX 6.5 Applications 2004 May.iso / dist / java3d.idb / usr / demos / java / j3d / programs / examples / Sound / ReverberateSound.java.z / ReverberateSound.java
Encoding:
Java Source  |  2003-08-08  |  7.4 KB  |  216 lines

  1. /*
  2.  *    @(#)ReverberateSound.java 1.17 02/10/21 13:54:32
  3.  *
  4.  * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  *
  13.  * - Redistribution in binary form must reproduce the above copyright
  14.  *   notice, this list of conditions and the following disclaimer in
  15.  *   the documentation and/or other materials provided with the
  16.  *   distribution.
  17.  *
  18.  * Neither the name of Sun Microsystems, Inc. or the names of
  19.  * contributors may be used to endorse or promote products derived
  20.  * from this software without specific prior written permission.
  21.  *
  22.  * This software is provided "AS IS," without a warranty of any
  23.  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
  24.  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
  25.  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
  26.  * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
  27.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  28.  * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
  29.  * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
  30.  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  31.  * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  32.  * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
  33.  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  34.  *
  35.  * You acknowledge that Software is not designed,licensed or intended
  36.  * for use in the design, construction, operation or maintenance of
  37.  * any nuclear facility.
  38.  */
  39.  
  40. /*
  41.  * ReverberateSound
  42.  *
  43.  * Same as MoveSound except this calls UniverseBuilderJS to use the
  44.  * JavaSoundMixer AudioDevice rather than the HolosketchMixer device.
  45.  *
  46.  * NOTE: To run this anywhere but the Solaris Eng Menlo Park network
  47.  * the URL path must be set to the java3d/javaone directory.
  48.  */
  49.  
  50. import java.applet.Applet;
  51. import java.awt.*;
  52. import java.awt.event.*;
  53. import com.sun.j3d.utils.applet.MainFrame;
  54. import com.sun.j3d.utils.geometry.ColorCube;
  55. import com.sun.j3d.utils.universe.*;
  56. import java.io.File;
  57. import javax.media.j3d.*;
  58. import javax.vecmath.*;
  59.  
  60. public class ReverberateSound extends Applet {
  61.  
  62.     // File name of sound sample
  63.     private static String[] filename = new String[1];
  64.     private static String path = null;
  65.     private static int filenamesGiven = 0;
  66.     private static boolean filenamesSet = false;
  67.  
  68.     private SimpleUniverse u = null;
  69.  
  70.     public BranchGroup createSceneGraph() {
  71.     // Create the root of the subgraph
  72.     BranchGroup objRoot = new BranchGroup();
  73.  
  74.     // Create the transform group node and initialize it to the identity.
  75.     // Enable the TRANSFORM_WRITE capability so that our behavior code
  76.     // can modify it at runtime.  Add it to the root of the subgraph.
  77.     TransformGroup objTrans = new TransformGroup();
  78.     objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  79.     objRoot.addChild(objTrans);
  80.  
  81.     // Create a simple shape leaf node and add it into the scene graph.
  82.     objTrans.addChild(new ColorCube(0.4));
  83.  
  84.     // Create a new Behavior object that will perform the desired
  85.     // operation on the specified transform object and add it into the
  86.     // scene graph.
  87.     Transform3D yAxis = new Transform3D();
  88.         Alpha rotation = new Alpha(-1, Alpha.INCREASING_ENABLE,
  89.                                    0, 0,
  90.                                    20000, 0, 0,
  91.                                    0, 0, 0);
  92.         RotationInterpolator rotator =
  93.             new RotationInterpolator(rotation,
  94.                                      objTrans, yAxis,
  95.                                      0.0f, (float) Math.PI*2.0f);
  96.     BoundingSphere bounds =
  97.         new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
  98.     rotator.setSchedulingBounds(bounds);
  99.     objTrans.addChild(rotator);
  100.  
  101.         //
  102.         // Create an AuralAttribute with reverb params set
  103.         //
  104.         Soundscape soundScape2 = new Soundscape();
  105.         AuralAttributes attributes2 = new AuralAttributes();
  106.         attributes2.setReverbOrder(6);
  107.         attributes2.setCapability(AuralAttributes.ALLOW_REVERB_ORDER_WRITE);
  108.         attributes2.setCapability(AuralAttributes.ALLOW_REVERB_DELAY_WRITE);
  109.         attributes2.setCapability(AuralAttributes.ALLOW_REFLECTION_COEFFICIENT_WRITE);
  110.         soundScape2.setApplicationBounds(bounds);
  111.         soundScape2.setAuralAttributes(attributes2);
  112.         objRoot.addChild(soundScape2);
  113.  
  114.         //
  115.         // Create a sound node and add it to the scene graph
  116.         //
  117.         PointSound sound = new PointSound();
  118.         sound.setCapability(PointSound.ALLOW_ENABLE_WRITE);
  119.         sound.setCapability(PointSound.ALLOW_INITIAL_GAIN_WRITE);
  120.         sound.setCapability(PointSound.ALLOW_SOUND_DATA_WRITE);
  121.         sound.setCapability(PointSound.ALLOW_DURATION_READ);
  122.         sound.setCapability(PointSound.ALLOW_POSITION_WRITE);
  123.         sound.setCapability(PointSound.ALLOW_LOOP_WRITE);
  124.         sound.setSchedulingBounds(bounds);
  125.  
  126.     objTrans.addChild(sound);
  127.         //
  128.     // Create a new Behavior object that will play the sound
  129.     //
  130.     AudioReverberate player = new AudioReverberate(sound, filename[0],
  131.                  attributes2);
  132.     player.setSchedulingBounds(bounds);
  133.     objTrans.addChild(player);
  134.  
  135.  
  136.     return objRoot;
  137.     }
  138.  
  139.     public ReverberateSound() { 
  140.     }
  141.  
  142.     public void init() {
  143.         if (!filenamesSet) {
  144.             // path is null if started from applet
  145.             if (path == null) {
  146.             // the path for an applet
  147.             path = getCodeBase().toString();
  148.             }
  149.     
  150.             /*
  151.              * append given file name to given URL path
  152.              */
  153.             if (filenamesGiven > 0) {
  154.                 filename[0] = new String(path + "/" + filename[0]);
  155.             }
  156.             else {
  157.                 // fill in default file names if all three not given
  158.                 filename[0] = new String(path + "/hello_universe.au");
  159.             }
  160.             filenamesSet = true;
  161.         }
  162.  
  163.     setLayout(new BorderLayout());
  164.         GraphicsConfiguration config =
  165.            SimpleUniverse.getPreferredConfiguration();
  166.  
  167.         Canvas3D c = new Canvas3D(config);
  168.     add("Center", c);
  169.  
  170.         /*
  171.          * Create a simple scene and attach it to the virtual universe
  172.          */
  173.         u = new SimpleUniverse(c);
  174.         AudioDevice audioDev = u.getViewer().createAudioDevice();
  175.         BranchGroup scene = createSceneGraph();
  176.  
  177.         // This will move the ViewPlatform back a bit so the
  178.         // objects in the scene can be viewed.
  179.         u.getViewingPlatform().setNominalViewingTransform();
  180.  
  181.     u.addBranchGraph(scene);
  182.     }
  183.  
  184.     public void destroy() {
  185.     u.cleanup();
  186.     }
  187.  
  188.     //
  189.     // The following allows ReverberateSound to be run as an application
  190.     // as well as an applet
  191.     //
  192.     public static void main(String[] args) {
  193.         if (args.length > 0) {
  194.             if ( (args[0].startsWith("file"+File.pathSeparator)) ||
  195.                  (args[0].startsWith("http"+File.pathSeparator))   ) {
  196.                 path = args[0];
  197.             }
  198.             else {
  199.                 path = "file:" + args[0];
  200.             }
  201.         }
  202.         else {
  203.             path = "file:.";
  204.         } 
  205.  
  206.         if (args.length > 1) {
  207.             filename[0] = args[1];
  208.             if (filename[0] != null) {
  209.                 filenamesGiven++ ;
  210.             }   
  211.         }
  212.  
  213.     new MainFrame(new ReverberateSound(), 256, 256);
  214.     }
  215. }
  216.